/*
 * Using inner class to avoid language dependencies. refer to ImageViewer.java
    String command  = event.getActionCommand();
        if (command.equals("Open")){ //what is in Malay, Open is Buka
 */

package chp11_GUI;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author Iskandar Sarkawi
 */
public class ImageViewer2_innerClass{

    private JFrame frame;
    private JLabel label;

    public ImageViewer2_innerClass(){
        makeFrame();
    }

    public void makeFrame(){
        frame = new JFrame("ImageViewer");
        makeMenuBar(frame);

        /*
         * There are 4 sections of a frame: title bar, menu bar, content pane, frame(holds everything)
         */
        Container contentPane = frame.getContentPane();

        label = new JLabel("I am a label");
        contentPane.add(label);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(350, 100);

        //frame.pack(); if this is on then it will overwrite the setSize
        frame.setVisible(true);
    }

    public void makeMenuBar(JFrame frame){
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        //Create the file menu
        JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);

        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(new OpenActionListener());
        fileMenu.add(openItem);

        JMenuItem quitItem = new JMenuItem("Quit");
        quitItem.addActionListener(new QuitActionListener());
        fileMenu.add(quitItem);
    }

    public static void main(String[] args) {

        ImageViewer2_innerClass vw = new ImageViewer2_innerClass();
    }

    /*
     * inner class
     */
    class OpenActionListener implements ActionListener{
        public void actionPerformed(ActionEvent event){            
                label.setText("Sub menu Open is clicked");
        }
    }

    class QuitActionListener implements ActionListener{

        public void actionPerformed(ActionEvent event){
                //label.setText("Sub menu Quit is clicked");
                System.exit(0);
        }

    }

}
